﻿using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows;
using MagicKey.Interfaces;
using MySql.Data.MySqlClient;
using MagicKey.Components.Database;

namespace MagicKey.Components.FileAccess
{
    public class FileAccess : IFileAccess
    {
        private string _password;
        private string _username;
        private MySqlCommand _cmd;
        private string _commandLine;
        private MySqlConnection _conn = null;
        MagicDatabase _myKeyMagicKeyDatabase = new MagicDatabase();

        public FileAccess()
        {
            //_myKeyMagicKeyDatabase.Connect();
        }

        /// <summary>
        /// </summary>
        /// <param name="password"></param>
        /// <param name="username"></param>
        public FileAccess(string password, string username)
        {
            _password = password;
            _username = username;
            //_myKeyMagicKeyDatabase.Connect();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="username"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public bool LoadFile(int userId, string username, string path)
        {
            bool loadSuccessfull = false;
            string encryptFile = null;

            IAccessManager am = new AccessManager.AccessManager();
            string key = _myKeyMagicKeyDatabase.LoadParameterUser("symetricKey", username);

            if (_myKeyMagicKeyDatabase.HasAuthorization(userId, path))
            {
                try
                {
                    var ftpHelper = new FtpHelper();
                    FileInfo file = ftpHelper.DownloadFile(path);
                    try
                    {
                        //encryptFile = am.DecryptFileSym(file.FullName, key);

                        //Process.Start(encryptFile);
                        loadSuccessfull = true;
                    }
                    catch (Exception e)
                    {
                        string msg = "Opening the file failed:\r\n " + encryptFile + " ! \r\n " + e.Message;
                        System.Windows.MessageBox.Show(msg, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }

            }
            return loadSuccessfull;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="path"></param>
        /// <param name="pathVers"></param>
        /// <param name="ownerId">The id of the owner</param>
        /// <returns>fullpath on the server</returns>
        public string UploadFile(string path, string pathVers, int ownerId)
        {
             bool isInserted = false;
            var file = new FileInfo(path);
            var ftpHelper = new FtpHelper();
           string pathOnServer = ftpHelper.SetUri(file.Name);

           this._myKeyMagicKeyDatabase.DatabaseName = "dba";
           isInserted = _myKeyMagicKeyDatabase.InsertFile(pathOnServer, ownerId);
           this._myKeyMagicKeyDatabase.DatabaseName = "dba2";
           isInserted = _myKeyMagicKeyDatabase.InsertFile(pathOnServer, ownerId);       

            if (isInserted)
            {
                if (ftpHelper.UploadFile(file))
                {
                    return pathOnServer;
                }
            }

            string msg = "Please check if the file does not already exist on the server.";
            MessageBox.Show(msg, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            return string.Empty;
        }

        public bool Exists(string uri)
        {
            return File.Exists(uri);
        }

        /// <summary>
        /// </summary>
        /// <param name="path"></param>
        /// <param name="ownerId"></param>
        /// <returns></returns>
        public bool DeleteFile(string path, int ownerId)
        {
            if (_myKeyMagicKeyDatabase.DeleteFile(path, ownerId))
            {
                var ftpHelper = new FtpHelper();
                if (ftpHelper.DeleteFile(new Uri(path)))
                    return true;
            }
            return false;
        }
    }
}